package mbms.mobilebudget.overview;

import java.math.RoundingMode;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.List;

import mbms.mobilebudget.R;
import mbms.mobilebudget.UserManager;
import mbms.mobilebudget.bankaccount.BankAccountManager;
import mbms.mobilebudget.creditCard.CreditCardManager;
import mbms.mobilebudget.database.DatabaseHandler;
import mbms.mobilebudget.entity.Exchange;
import mbms.mobilebudget.entity.User;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;

@SuppressLint("ShowToast")
public class OverviewActivity  extends OrmLiteBaseActivity<DatabaseHandler> {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_overview);
		
		Intent intent = getIntent();
		User user = (User) intent.getSerializableExtra("loggedInUser");
		if(user==null){
			Toast.makeText(getApplicationContext(), "User not acquired", Toast.LENGTH_LONG).show();
			return ;
		}
		
		UserManager userManager = new UserManager(getHelper(), getApplicationContext(), user); 
		BankAccountManager bankAccountManager = new BankAccountManager(getHelper(), getApplicationContext());
		CreditCardManager creditCardManager = new CreditCardManager(getHelper(), getApplicationContext());
		
		Dao<Exchange, Integer> exchange2Dao;
		List<Exchange> rates = null;
		try {
			exchange2Dao = getHelper().getExchangeDao();
			rates = exchange2Dao.queryForAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Overview overview = new Overview(bankAccountManager, creditCardManager, userManager.getBankAccountList(), userManager.getCreditCardList(), rates);
		NumberFormat numberInstance = NumberFormat.getNumberInstance();
		numberInstance.setMaximumFractionDigits(2);
		numberInstance.setRoundingMode(RoundingMode.HALF_UP);
		
		((TextView)findViewById(R.id.total_budget)).setText(numberInstance.format(overview.getTotalBudget()));
		((TextView)findViewById(R.id.total_income)).setText(numberInstance.format(overview.getTotalIncome()));
		((TextView)findViewById(R.id.total_expense)).setText(numberInstance.format(overview.getTotalExpense()));
		
		((TextView)findViewById(R.id.total_credit_card_expense)).setText(numberInstance.format(overview.getCreditCardTotalExpenditure()));
		((TextView)findViewById(R.id.total_bank_account_expense)).setText(numberInstance.format(overview.getBankAccountTotalExpenditure()));
		
		((TextView)findViewById(R.id.total_salary)).setText(numberInstance.format(overview.getTotalSalaryIncome()));
		((TextView)findViewById(R.id.total_rent)).setText(numberInstance.format(overview.getTotalSalaryIncome()));
		((TextView)findViewById(R.id.total_bonus)).setText(numberInstance.format(overview.getTotalSalaryIncome()));
		
		((TextView)findViewById(R.id.total_food_eating)).setText(numberInstance.format(overview.getTotalFoodEatingExpenditure()));
		((TextView)findViewById(R.id.total_transportation)).setText(numberInstance.format(overview.getTotalTransportationExpenditure()));
		((TextView)findViewById(R.id.total_amusement)).setText(numberInstance.format(overview.getTotalAmusementExpenditure()));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.bank_accounts, menu);
		return true;
	}
	
	public void backButtonClicked(View view){
		super.onBackPressed();
	} 
	
   @Override
    public void onResume()
    {
        super.onResume();
    }
}


